﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace caufingHealth
{
    public class HealthManager : Imanager
    {

        private IData DataManager = null;
        private int loginID;

        public HealthManager()
        {
            DataManager = new InformationRetriever();
        }


        public bool Login(string userName, string password)
        {
            try
            {
               if(DataManager.Authenticate(int.Parse(userName), password))
               {
                   loginID = int.Parse(userName);
                   return true;
               }
               else
               {
                   return false;
               }
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show("An Exception Occured:" + e.Message );
                return false;
            }
        }

        //TODO: all this!
        public PatientInformation.PatientRecord SearchForPatientRecords(string FirstName, string LastName)
        {
            PatientInformation.PatientRecord[] results = DataManager.suchen(FirstName, LastName);
            if (results.Length > 1)
                return SelectFromMultipleRecords(results);
            if (results.Length == 0)
                return null;
            return results[0];
            /**
            PatientInformation.PatientRecord returnrecord = new PatientInformation.PatientRecord();
            
             //returnrecord.SetName(1,FirstName, LastName);
            returnrecord.SetAddress(4285, "Anderson St.", "Douglasville", "GA", 76543);
            returnrecord.SetDateOfBirth(12, 12, 2009);
            returnrecord.SetContactNumber(404, 555, 5687);
           // return returnrecord;

            List<KeyValuePair<string, object>>[] results = DataManager.Search(FirstName, LastName);
            if (results.Length > 1)
            {
                return SelectFromMultipleRecords(results);
                
            }
            if (results.Length == 0)
                return null;

            return ParseRecord(results[0]);    **/       
        }

        private PatientInformation.PatientRecord SelectFromMultipleRecords(List<KeyValuePair<string, object>>[] results)
        {
            Forms.SearchResults search = new Forms.SearchResults(results);
            if (search.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                return ParseRecord(DataManager.GetPatientRecord(search.SelectedID)[0]);
            }
            else
            {
                return null;
            }
        }

        private PatientInformation.PatientRecord SelectFromMultipleRecords(PatientInformation.PatientRecord[] results)
        {
            Forms.SearchResults search = new Forms.SearchResults(results);
            if (search.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                return ParseRecord(DataManager.GetPatientRecord(search.SelectedID)[0]);
            }
            else
            {
                return null;
            }
        }

        private PatientInformation.PatientRecord ParseRecord( List<KeyValuePair<string, object>> record)
        {
            PatientInformation.PatientRecord returnrecord = new PatientInformation.PatientRecord();
            string firstName = null;
            string lastName = null;
            int streetnum = 0;
            string streetname = null;
            string city = null;
            string state = null;
            int zipcode = 0;
            int id = 0;
            foreach (KeyValuePair<string, object> pair in record)
            {
                if (pair.Key.Equals("FirstName"))
                    firstName = (string)pair.Value;
                else if (pair.Key.Equals("LastName"))
                    lastName = (string)pair.Value;
                else if (pair.Key.Equals("id"))
                    id = (int)pair.Value;
                else if (pair.Key.Equals("Street#"))
                    streetnum = (int)pair.Value;
                else if (pair.Key.Equals("StreetName"))
                    streetname = (string)pair.Value;
                else if (pair.Key.Equals("City"))
                    city = (string)pair.Value;
                else if (pair.Key.Equals("State"))
                    state = (string)pair.Value;
                else if (pair.Key.Equals("ZipCode"))
                    zipcode = (int)pair.Value;
                else if (pair.Key.Equals("DateOfBirth"))
                {
                    DateTime dob = new DateTime((long)pair.Value);
                    returnrecord.SetDateOfBirth(dob.Month, dob.Day, dob.Year);
                }
                else if (pair.Key.Equals("Contact#"))
                {
                    returnrecord.SetContactNumber(int.Parse(((string)pair.Value).Substring(0, 3)),
                                                  int.Parse(((string)pair.Value).Substring(3, 3)),
                                                  int.Parse(((string)pair.Value).Substring(6)));
                }

            }
            returnrecord.SetName(id, firstName, lastName);
            returnrecord.SetAddress(streetnum, streetname, city, state, zipcode);
            return returnrecord;
        }

        public PatientInformation.PatientRecord SearchForPatientRecords(string LastName, DateTime DOB)
        {
            List<KeyValuePair<string, object>>[] results = DataManager.Search(LastName, DOB.Date);
            if (results.Length > 1)
            {
                return SelectFromMultipleRecords(results);
            }

            return ParseRecord(results[0]);
        }

        public PatientInformation.PatientRecord SearchForPatientRecords(DateTime DOB)
        {
            List<KeyValuePair<string, object>>[] results = DataManager.Search(DOB.Date);
            if (results.Length > 1)
            {
                return SelectFromMultipleRecords(results);
            }
            if (results.Length == 0)
                return null;
            return ParseRecord(results[0]);
        }

        public bool AddPatientRecord(PatientInformation.PatientRecord pRecord)
        {
            return DataManager.AddPatientRecord(pRecord);
        }

        public PatientInformation.PatientRecord.PatientVisitRecord[] GetPatientVisitRecord(int ID)
        {
            return DataManager.getPatientVisitRecord(ID);
        }

        public PatientInformation.PatientRecord.PatientLabRecord[] GetPatientLabRecord(long visitDate, int patientID)
        {
            return DataManager.getPatientLabRecord(visitDate, patientID);
        }
        
        public bool AddNewPatientVisitRecord(int patientID, PatientInformation.PatientRecord.PatientVisitRecord visit)
        {
            return DataManager.addPatientVisitRecord(loginID, DateTime.Now, patientID, visit) > 0 ? true : false;
        }

        public bool updatePatientVisitRecord(int PatientID, PatientInformation.PatientRecord.PatientVisitRecord updatedPatientVisitData)
        {
            return DataManager.updatePatientVisitRecord(PatientID, DateTime.Now, updatedPatientVisitData) > 0 ? true : false;
        }

        public bool deletePatientVisitRecord(int patientID, long date)
        {
            return DataManager.deletePatientVisitRecord(patientID, date) > 0 ? true : false;
        }

        public DoctorInformation[] GetDoctorInformation()
        {
            return DataManager.GetDoctorInformation();
        }

        public LabTestInformation[] GetAvailableTests()
        {
            return DataManager.GetAvailableTests();
        }

        public bool addPatientLabRecord(int patientID, int doctorID, long visitDateLong, int type, int result)
        {
            return DataManager.addPatientLabRecord(loginID, patientID, doctorID, visitDateLong, type, result) > 0 ? true : false;
        }

        public bool updatePatientLabRecord(int patientID, int doctorID, long date, int type, int result)
        {
            return DataManager.updatePatientLabRecord(loginID, patientID, doctorID, date, type, result) == 0 ? false : true;
        }

        public System.Data.DataTable AdminQuery(string query)
        {
            if (query == null || String.IsNullOrWhiteSpace(query))
                throw new ArgumentException("The query must not be empty, null or simply whitespace.");
            return DataManager.AdminQuery(query);
        }

        public bool isAdmin()
        {
            return DataManager.isAdmin(loginID);
        }
    }
}
